package com.xuedongzhilian.controller;

import com.xuedongzhilian.dto.Result;
import com.xuedongzhilian.entity.PointsProp;
import com.xuedongzhilian.service.IPointsPropService;
import com.xuedongzhilian.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 积分道具控制器
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Slf4j
@RestController
@RequestMapping("/points-prop")
public class PointsPropController {

    @Resource
    private IPointsPropService pointsPropService;
    
    private PointsProp createShopProp(String propType, String propName, Integer points) {
        PointsProp prop = new PointsProp();
        prop.setPropType(propType);
        prop.setPropName(propName);
        prop.setPoints(points);
        return prop;
    }

    /**
     * 获取用户积分道具列表
     */
    @GetMapping
    public Result getUserPointsProps() {
        Long userId = UserHolder.getUser().getId();
        List<PointsProp> pointsProps = pointsPropService.lambdaQuery()
                .eq(PointsProp::getUserId, userId)
                .orderByDesc(PointsProp::getCreateTime)
                .list();
        return Result.ok(pointsProps);
    }

    /**
     * 使用积分道具
     */
    @PostMapping("/use/{id}")
    public Result usePointsProp(@PathVariable Long id) {
        Long userId = UserHolder.getUser().getId();
        PointsProp pointsProp = pointsPropService.getById(id);
        
        if (pointsProp == null) {
            return Result.fail("道具不存在");
        }
        
        if (!pointsProp.getUserId().equals(userId)) {
            return Result.fail("无权使用该道具");
        }
        
        if (pointsProp.getQuantity() <= 0) {
            return Result.fail("道具数量不足");
        }
        
        if (pointsProp.getExpireTime() != null && pointsProp.getExpireTime().isBefore(LocalDateTime.now())) {
            return Result.fail("道具已过期");
        }
        
        // 减少道具数量
        pointsProp.setQuantity(pointsProp.getQuantity() - 1);
        pointsProp.setUpdateTime(LocalDateTime.now());
        
        boolean success = pointsPropService.updateById(pointsProp);
        if (success) {
            return Result.ok("使用成功");
        } else {
            return Result.fail("使用失败");
        }
    }

    /**
     * 获取道具商店列表
     */
    @GetMapping("/shop")
    public Result getPropShop() {
        // 预定义的商店道具
        List<PointsProp> shopProps = Arrays.asList(
            createShopProp("专注药水", "专注药水", 100),
            createShopProp("耐力符文", "耐力符文", 150),
            createShopProp("双倍卡", "双倍卡", 200)
        );
        return Result.ok(shopProps);
    }

    /**
     * 兑换道具
     */
    @PostMapping("/exchange")
    public Result exchangeProp(@RequestParam String propName, @RequestParam Integer quantity) {
        Long userId = UserHolder.getUser().getId();
        // TODO 实现积分扣除和道具发放逻辑
        return Result.ok("兑换成功");
    }
}